import Vue from 'vue';
import Vuex from 'vuex';
Vue.use(Vuex);
// var url = 'http://wz-wood.caizhiku.cn/web/'
var url = 'https://wsgdqx.wzheiying.com:8083/'
// var url = '/ZHU' //测试
// var url2 = 'https://qqm.wzheiying.com/warning/'//生产
var url2 = ''//生产2

export default new Vuex.Store({
  state: {
    count: 1,
    token: '',
    loadingShow: false, //加载动画
    // http://jx.vaiwan.com/web/
    baseUrl: process.env.NODE_ENV == 'development' ? url : url2,
    openUrl: process.env.NODE_ENV == 'development' ? url2 : url2, //本地打开木材老数据用域名
    newRoutes: [], //动态路由数组
    menuList: [], //导航栏菜单
    thirdMenu: [], //三级路由
    typeAllList: [], //全部基础数据列表
    contractAllList: [], //合同列表
    operateShow: true, //列表的操作列的显示
    goodsType: [
      "预保公司",
      "仓库员",
      "复核员",
      "核算员",
      "合同类型",
      "客户名称",
      "附件类型",
      "树种",
      "集团分类",
      "收汇摘要",
      "商品名称",
      "商品类型",
      "收汇用途",
      "退汇用途",
      "退汇单位",
      "船名",
      "原产地国",
      "起运国",
      "目的港",
      "装货港",
      "出口口岸",
      "仓库属性",
      "仓库类型",
      "单据类型",
      "信用证类型",
      "装运方式",
      "计价方式",
      "税款名称",
      "体积单位",
      "利息类型",
      "银行费用名称",
      "货柜尺寸",
      "重量单位",
      "品种类别",
      "银行名称",
      "银行类型",
      "进口口岸",
      "报关单位",
      "运输方式",
      "贸易方式",
      "币种",
      "扣尺方法",
      "价格条款",
      "融资形式",
      "付款方式",
      "付款方式(付款管理)",
      "还款方式",
      "公司类型",
      "受益人",
      "数量单位",
      "转口形式",
    ],
    townList: [],
    villageList: [],
    meanTotal: {}, //设置菜单待确认数量
    mapArea: [
      {
        center: "{\"lng\":\"120.5685\",\"lat\":\"27.6776\"}",
        communityId: 2132,
        communityName: "新城社区",
        count: 0,
        gridName: "联华社区",
        id: 11,
        mapArea: "[{\"lng\":\"120.5685\",\"lat\":\"27.67758\"}]",
        secondGridId: 103,
        secondGridName: "第1微网格",
        secondGridUserName: "薛树煜",
        mapColor: "#00B2FF",
      },

    ],
    listAllList: [],//全部微网格接口数据
    listAllArea: [],//全部微网格数据
    listAllAreaCenter: [],//全部微网格全科中心点数据
  },
  mutations: {
    /**
     * @desc 设置路由数组
     * @param  newRoutes 新动态路由 
     * @param  menuList 导航栏菜单 
     */
    SET_MENULIST(state, res) {
      state.newRoutes = res.newRoutes
      state.menuList = res.menuList
    },
    getMapArea(state, mapArea) {
      state.mapArea = mapArea
    }

  },
  actions: {
    GET_MAP_AREA(context, value) { //函数接受一个与 store 实例具有相同方法和属性的 context 对象
      console.log(context, 111)
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          let mapArea = context.state.mapArea
          if (value) {
            mapArea.push({
              latitude: value.mapCenter.lat,
              longitude: value.mapCenter.lng,
              mapArea: JSON.stringify(value.polygonLatList),
              mapColor: value.mapColor
            })
          }
          // context.commit('getMapArea', mapArea)
          resolve(mapArea)
          if (context.state.mapArea.length == 0) {
            reject('暂无数据')
          }
        }, 500)
      })
    },
    /**
     * @desc 设置动态路由数组
     * @param 
     * 数据库配置路由注意事项
     * 一级路由只用配置路由名和id
     * 
     * 二级路由需要id name path component path代表vue访问路径 
     * component路由模板代表在page下的页面路径
     * 
     * 各个信息页面和标签栏需要归为三级路由 只有三级路由的 hiden_in_menu 是有效的
     */
    SET_MENULIST({
      commit
    }, marketInfo) {
      var menuList = window.localStorage.userList ? JSON.parse(window.localStorage.userList).menuList : []
      var accessRoutes = [] //路由数组
      console.log('menuList', menuList)
      menuList.map((item, index) => {
        //一级路由有子路由的不分配路由地址
        if (item.childList.length == 0) { //一级路由
          accessRoutes.push({
            path: item.path,
            name: item.name,
            meta: {
              keepAlive: item.keepAlive,
            },
            component: (resolve) => require([`@/components/page${item.component}`], resolve)

          })
        } else {
          //二级路由有无子路由都分配地址 
          item.childList.map((item2, index2) => { //二级路由
            accessRoutes.push({
              path: item2.path,
              name: item2.name,
              meta: {
                keepAlive: item2.keepAlive,
              },
              component: (resolve) => require([`@/components/page${item2.component}`], resolve)
            })
            if (item2.childList.length == 0) {
              // console.log('accessRoutes1', accessRoutes)
            } else {
              //三级路由有无子路由都分配地址 
              item2.childList.map((item3, index3) => { //三级路由
                accessRoutes.push({
                  path: item3.path,
                  name: item3.name,
                  meta: {
                    keepAlive: item3.keepAlive,
                  },
                  component: (resolve) => require([`@/components/page${item3.component}`], resolve)
                })
              })
            }
          })
        }
      })
      console.log('accessRoutes', accessRoutes)
      accessRoutes.push({ //最后添加404页面 防止页面刷新空白
        path: '*',
        component: (resolve) => require([`@/components/page/notFound`], resolve),
        meta: {
          historyRoute: false,
          topTabs: false,
          leftNav: false,
        }
      })

      commit({
        type: "SET_MENULIST",
        newRoutes: accessRoutes,
        menuList: menuList
      });
    },

  },
})
